package com.zhang.gulimall.product.service.impl;

import com.mysql.cj.util.StringUtils;
import com.zhang.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zhang.gulimall.product.dao.AttrDao;
import com.zhang.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zhang.gulimall.product.entity.AttrEntity;
import com.zhang.gulimall.product.service.AttrAttrgroupRelationService;
import com.zhang.gulimall.product.service.AttrService;
import com.zhang.gulimall.product.vo.AttrGroupVo;
import com.zhang.gulimall.product.vo.AttrVo;
import com.zhang.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.Query;

import com.zhang.gulimall.product.dao.AttrGroupDao;
import com.zhang.gulimall.product.entity.AttrGroupEntity;
import com.zhang.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrAttrgroupRelationService relationService;
    @Resource
    AttrService attrService;
    @Resource
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, String catId) {
        if (catId.equals("0")){
            return queryPage(params);
        } else {
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(AttrGroupEntity::getCatelogId, catId);
            String key = (String) params.get("key");
            if (!StringUtils.isEmptyOrWhitespaceOnly(key)){
                wrapper.lambda().eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            }
            return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params),wrapper));
        }
    }

    @Override
    public List<AttrEntity> queryRelationPage(Map<String, Object> params, String groupId) {
        List<AttrEntity> list1 = new ArrayList<>();
        List<AttrAttrgroupRelationEntity> list = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, groupId));
        if (list.size() > 0) {
            list.forEach(l -> list1.add(attrService.getById(l.getAttrId())));
        }
        return list1;
    }

    @Override
    public List<AttrEntity> queryNoRelationPage(Map<String, Object> params, String groupId) {
        AttrGroupEntity entity = this.baseMapper.selectById(groupId);
        List<AttrAttrgroupRelationEntity> list = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, groupId));
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AttrEntity::getCatelogId,entity.getCatelogId()).eq(AttrEntity::getAttrType,1);
        String key = (String) params.get("key");
        if (!key.isEmpty()){
            wrapper.lambda().like(AttrEntity::getAttrId,key)
                    .or().like(AttrEntity::getAttrName,key)
                    .or().like(AttrEntity::getAttrValue,key);
        }
        List<AttrEntity> list1 = attrService.list(wrapper);
        for (AttrAttrgroupRelationEntity l1 : list) {
            list1 = list1.stream().filter(l2-> !l1.getAttrId().equals(l2.getAttrId())).distinct().collect(Collectors.toList());
        }
        return list1;
    }

    @Override
    public void saveRelations(List<AttrVo> attrVos) {
        for (AttrVo attrVo : attrVos) {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrVo,entity);
            relationService.save(entity);
        }
    }

    @Override
    public void removeRelation(List<AttrVo> attrVos) {
        for (AttrVo attrVo : attrVos) {
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(AttrAttrgroupRelationEntity::getAttrId, attrVo.getAttrId());
            relationService.removeById(wrapper);
        }
    }

    @Override
    public List<AttrGroupVo> groupWithAttrList(Map<String, Object> params, String catelogId) {
        List<AttrGroupVo> list = new ArrayList<>();
        List<AttrGroupEntity> list1 = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().lambda().eq(AttrGroupEntity::getCatelogId, catelogId));
        if (list1.size()>0){
            for (AttrGroupEntity l1 : list1) {
                AttrGroupVo attrGroupVo = new AttrGroupVo();
                BeanUtils.copyProperties(l1,attrGroupVo);
                List<AttrAttrgroupRelationEntity> l2 = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, l1.getAttrGroupId()));
                for (AttrAttrgroupRelationEntity l3 : l2) {
                    AttrEntity attrEntity = attrService.getById(l3.getAttrId());
                    if (attrEntity!=null){
                        if (attrGroupVo.getAttrs()!=null){
                            attrGroupVo.getAttrs().add(attrEntity);
                        } else {
                            attrGroupVo.setAttrs(
                                    new ArrayList<AttrEntity>(){{
                                    this.add(attrEntity);
                                }}
                            );
                        }
                    }
                }
                list.add(attrGroupVo);
            }
        }
        return list;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroup> getAttrGroupWithAttrsBySpuId(String spuId, String catalogId) {
        List<SkuItemVo.SpuItemAttrGroup> spuItemAttrGroups;

        // 查询属性分组
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AttrGroupEntity::getCatelogId, catalogId);
        List<AttrGroupEntity> attrGroups = attrGroupService.list(queryWrapper);

        // 查询属性信息
        QueryWrapper<AttrEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(AttrEntity::getCatelogId, catalogId).eq(AttrEntity::getAttrType, 1);
        List<AttrEntity> attrs = attrService.list(queryWrapper1);

        // 查询属性关系
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper2 = new QueryWrapper<>();
        List<String> collect = attrGroups.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        queryWrapper2.lambda().in(AttrAttrgroupRelationEntity::getAttrGroupId, collect);
        List<AttrAttrgroupRelationEntity> relationEntityList = relationService.list(queryWrapper2);

        // 组合
        spuItemAttrGroups = attrGroups.stream().map(group -> {
            SkuItemVo.SpuItemAttrGroup spuItemAttrGroup = new SkuItemVo.SpuItemAttrGroup();
            // 先筛选出当前分组的属性
            List<AttrAttrgroupRelationEntity> collect1 = relationEntityList.stream().filter(relation ->
                    relation.getAttrGroupId().equals(group.getAttrGroupId())).collect(Collectors.toList());
            // 找到当前分组的属性
            List<AttrEntity> attrList = new ArrayList<>();
            collect1.forEach(c1->{
                AttrEntity attrEntity = attrs.stream().filter(attr -> attr.getAttrId().equals(c1.getAttrId())).collect(Collectors.toList()).get(0);
                attrList.add(attrEntity);
            });
            spuItemAttrGroup.setGroupName(group.getAttrGroupName());
            spuItemAttrGroup.setAttrs(attrList);
            return spuItemAttrGroup;
        }).collect(Collectors.toList());

        return spuItemAttrGroups;
    }

}